package com.huidu.bitrade.service;

import com.huidu.bitrade.constant.DepositStatusEnum;
import com.huidu.bitrade.constant.PageModel;
import com.huidu.bitrade.dao.DepositRecordDao;
import com.huidu.bitrade.entity.DepositRecord;
import com.huidu.bitrade.entity.Member;
import com.huidu.bitrade.entity.QDepositRecord;
import com.huidu.bitrade.entity.QMember;
import com.huidu.bitrade.service.Base.BaseService;
import com.huidu.bitrade.vo.DepositRecordVO;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author QQ：97095646
 * @date 2019/5/7
 */
@Service
public class DepositRecordService extends BaseService {
    @Autowired
    private DepositRecordDao depositRecordDao;

    public List<DepositRecord> getAll() {
        return depositRecordDao.findAll();
    }

    public Page<DepositRecord> list(Predicate predicate, PageModel pageModel) {
        return depositRecordDao.findAll(predicate, pageModel.getPageable());
    }

    public List<DepositRecord> findAll(Predicate predicate) {
        return (List<DepositRecord>) depositRecordDao.findAll(predicate);
    }

    public DepositRecord findOne(String id) {
        return depositRecordDao.findById(id);
    }

    public void update(DepositRecord depositRecord) {
        depositRecordDao.save(depositRecord);
    }

    public void create(DepositRecord depositRecord) {
        depositRecordDao.save(depositRecord);
    }


    public List<DepositRecord> findByMemberAndStatusAndType(Member member, DepositStatusEnum status, Integer type) {
        return depositRecordDao.findByMemberAndStatusAndType(member, status, type);
    }

    public DepositRecord save(DepositRecord depositRecord) {
        return depositRecordDao.save(depositRecord);
    }

    public Page<DepositRecordVO> joinFind(List<Predicate> predicates, PageModel pageModel) {
        JPAQuery<DepositRecordVO> query = queryFactory.select(
                Projections.fields(DepositRecordVO.class,
                        QDepositRecord.depositRecord.id.as("id"),
                        QDepositRecord.depositRecord.member,
                        QDepositRecord.depositRecord.amount.as("amount"),
                        QDepositRecord.depositRecord.createTime.as("createTime"),
                        QDepositRecord.depositRecord.status
                )
        ).from(QDepositRecord.depositRecord, QMember.member).where(predicates.toArray(new Predicate[0]));
        query.orderBy(new OrderSpecifier<>(Order.DESC, QDepositRecord.depositRecord.createTime));
        List<DepositRecordVO> list = query.offset((long) (pageModel.getPageNo() - 1) * pageModel.getPageSize()).limit(pageModel.getPageSize()).fetch();
        long total = query.fetchCount();
        return new PageImpl<>(list, pageModel.getPageable(), total);
    }
}
